﻿using System;
using System.Collections.Generic;
using System.Text;

namespace UtilZ.Dotnet.Ex.Communication.Base
{
    /// <summary>
    /// 通信客户端基类
    /// </summary>
    public abstract class ClientAbs : IClient
    {
        /// <summary>
        /// 始化参数
        /// </summary>
        protected IClientInitPara _initPara;

        /// <summary>
        /// summary
        /// </summary>
        public IClientInitPara InitPara
        {
            get { return this._initPara; }
        }



        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="initPara">始化参数</param>
        public ClientAbs(IClientInitPara initPara)
        {
            if (initPara == null)
            {
                throw new ArgumentNullException(nameof(initPara));
            }

            this._initPara = initPara;
        }


        /// <summary>
        /// Tag
        /// </summary>
        public object Tag { get; set; }


        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="sendDataOptions">发送的数据选项</param>
        public void Send(ISendDataOptions sendDataOptions)
        {
            this.PrimitiveSend(sendDataOptions);
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="sendDataOptions">发送的数据选项</param>
        protected abstract void PrimitiveSend(ISendDataOptions sendDataOptions);



        /// <summary>
        /// 
        /// </summary>
        protected readonly object _disposeLock = new object();

        /// <summary>
        /// 对象释放标识[true:已释放;false:未释放]
        /// </summary>
        protected bool _disposed = false;
        /// <summary>
        /// 获取当前对象是否已释放[true:已释放;false:未释放]
        /// </summary>
        public bool Disposabled
        {
            get
            {
                return _disposed;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
            lock (this._disposeLock)
            {
                if (this._disposed)
                {
                    return;
                }
                this._disposed = true;

                this.Dispose(true);
            }
        }

        /// <summary>
        /// Dispose
        /// </summary>
        /// <param name="disposing"></param>
        protected virtual void Dispose(bool disposing)
        {
            this._initPara = null;
        }
    }
}
